/*
 * @Description: 档案类型层级树结构组件
 * @Author: Rfan
 * @Date: 2022-01-20 14:38:03
 * @LastEditTime: 2022-01-20 17:19:56
 */

import { Tree } from 'antd';
import { useMetadataContext } from '../contexts/MetadataConfigContext';

const fieldNames = { title: 'treeName', key: 'key', children: 'children' };

const ATTree = () => {
  const { ATList, fetchATLevel, fetchMateData } = useMetadataContext();

  const onLoadData = ({ id }: any) =>
    new Promise<void>((resolve) => {
      fetchATLevel(id, resolve);
    });

  const selectNode = (key: any, { node }: any) => {
    if (node.tableName) {
      fetchMateData(node.tableName);
    }
  };

  return (
    <Tree
      loadData={onLoadData}
      treeData={ATList}
      fieldNames={fieldNames}
      showLine={true}
      onSelect={selectNode}
    />
  );
};

export default ATTree;
